home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11220 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Beginer C please help me
  5. Date: 22 Mar 1996 12:12:45 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4iun2d$896@umbc9.umbc.edu>
  8. References: <DoLCFx.B7x.0.bloor@torfree.net>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Sherif Asif <bz786@torfree.net> wrote:
  13. |> This may be a stupid mistake but "please help" me.
  14. |> Thank you very much.
  15. |> 
  16. |> #include <stdio.h>
  17. |> #include <math.h>
  18. |> main ()
  19. |> {
  20. |> float n;
  21. |> n=(9/5);
  22.  
  23. The calculation 9/5 is an integral calculation. Since 5 only goes into
  24. 9 one time (with a remainder that gets discarded) your answer is 1.
  25. Perhaps you have mistakenly assumed that since the result is being assigned
  26. to a float that something magical would happen? The only way to get the
  27. correct answer is to perform the calculation in floating point. Either
  28. a cast or using explicit floating point constants will do the trick.
  29.  
  30. |> printf ("n=   %1.3f\n", n);
  31.  
  32. You may want a 'return (0);' here as well.
  33.  
  34. |> }
  35. |> I have complied and run the programme & I got the answer n=1.000
  36. |> but Iam supposed to get answer 1.8.
  37. |> Why I got this answer?
  38.  
  39. Make the 9/5 to 9.0/5.0.
  40. -- 
  41. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  42.  
  43. Jonas J. Schlein  (schlein@gl.umbc.edu)
  44.